<?php

/**
 * ECSHOP 专题前台
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @author:     webboy <laupeng@163.com>
 * @version:    v2.1
 * ---------------------------------------------
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

// if ($_REQUEST['act'] != 'buy' && strpos($_SERVER['REQUEST_URI'],'.php')!==false){
// 	jump_404();
// }

if (empty($_REQUEST['act']))
{
	$_REQUEST['act'] = 'index';
}


if ($_REQUEST['act'] == 'index')
{
	$templates = 'cat_promote.dwt';
	$page   = !empty($_REQUEST['page'])  && intval($_REQUEST['page'])  > 0 ? intval($_REQUEST['page'])  : 1;
	/* 获得总数 */
	$size   = 20;
	$count = $db->getOne("SELECT COUNT(1) FROM " . $ecs->table('cat_promote') . " WHERE is_show = 1");
	$pages  = ($count > 0) ? ceil($count / $size) : 1;
	
	if ($page > $pages)
	{
		$page = $pages;
	}
	$list = cat_promote_list($page,$size);
	assign_pager('cat_promote', $cat_id, $count, $size, '', '', $page);
	$smarty->assign('list',				$list);
	$position = assign_ur_here();
	$smarty->assign('page_title',       $position['title']);    // 页面标题
	$smarty->assign('ur_here',          $position['ur_here']);  // 当前位置
	$smarty->assign('categories',       get_categories_tree_pro()); // 分类树
	$smarty->assign('helps',            get_shop_help());              // 网店帮助
	assign_template();
	$smarty->assign('cfg', $_CFG);	
}
elseif ($_REQUEST['act'] == 'view')
{
	$templates = 'cat_promote_view.dwt';
	$id = intval($_GET['id']);
	$info = cat_promote_info($id);
	if (!$info){
		show_message('病症不存在','','','error');
	}
	
	//模板赋值
	$smarty->assign('info',$info);
	$smarty->assign('cfg', $_CFG);
	assign_template();
	$position = assign_ur_here(0, $info['name']);
	$smarty->assign('page_title', $position['title']);    // 页面标题
	$smarty->assign('ur_here',    $position['ur_here']);  // 当前位置
	$smarty->assign('categories',       get_categories_tree_pro()); // 分类树
	$smarty->assign('helps',            get_shop_help());              // 网店帮助
	assign_template();

}

/* 显示模板 */
$smarty->display($templates);

/**
 * 医师推荐列表
 * @param number $page
 * @param number $size
 * @return Ambigous <multitype:, unknown>
 */
function cat_promote_list($page = 1, $size = 20)
{

	$sql = "SELECT promote_id,name,description,add_time,img FROM ".$GLOBALS['ecs']->table('cat_promote').
			" WHERE is_show = 1 ORDER BY sort_order DESC,add_time DESC";

	$res = $GLOBALS['db']->selectLimit($sql, $size, ($page-1) * $size);

	$arr = array();
	if ($res)
	{
		while ($row = $GLOBALS['db']->fetchRow($res))
		{
			$promote_id = $row['promote_id'];
			$arr[$promote_id]['url']			= 'cat_promote.php?act=view&amp;id='.$promote_id;
			$arr[$promote_id]['add_time']		= date($GLOBALS['_CFG']['date_format'], $row['add_time']);
			$arr[$promote_id]['name']			= $row['name'];
			$arr[$promote_id]['description']	= $row['description'];
			$arr[$promote_id]['img']			= $row['img'];
		}
	}

	return $arr;
}

/**
 * 病症详情
 * @param unknown $id
 * @return 
 */
function cat_promote_info($id)
{
	$sql = "SELECT promote_id,name,description,group_id,goods_id,detail,add_time FROM ".$GLOBALS['ecs']->table('cat_promote').
			" WHERE promote_id = $id AND is_show = 1";
	$info = $GLOBALS['db']->getRow($sql);
	if ($info){
		$goods = get_cat_promote_goods($info['goods_id'], $info['group_id']);
		$goods_list = array();
		if ($goods){
			$goods_list[] = array(
					'goods_name'=>$goods[0]['parent_name'],
					'goods_thumb'=>get_image_path($goods[0]['parent_id'],$goods[0]['parent_thumb'],true),
					'goods_price'=>price_format($goods[0]['parent_price']),
					'url'=>build_uri('goods', array('gid' => $goods[0]['parent_id']))
			);
			foreach ($goods as $kk=>$vv){
				if ($vv['goods_name']){
					$goods_list[] = array(
							'goods_name'=>$vv['goods_name'],
							'goods_thumb'=>get_image_path($vv['goods_id'],$vv['goods_thumb'],true),
							'goods_price'=>price_format($vv['goods_price']),
							'url'=>build_uri('goods', array('gid' => $vv['goods_id']))
					);
				}
			}
		}
		
		$info['goods_list'] = $goods_list;
		$info['add_time'] = date($GLOBALS['_CFG']['date_format'], $info['add_time']);
	}
		
	return $info;
}

?>